package com.datounet.flutterapp.methodChannel;

import android.content.Intent;
import android.widget.Toast;

import com.datounet.flutterapp.AgentWebActivity;
import com.datounet.flutterapp.Bean.BeanUser;
import com.datounet.flutterapp.BuglyManager;
import com.datounet.flutterapp.Locat.LocationMgr;
import com.datounet.flutterapp.MainActivity;
import com.datounet.flutterapp.MapActivity;
import com.datounet.flutterapp.MyApplication;
import com.datounet.flutterapp.NativeTTSMgr;
import com.datounet.flutterapp.WebSocket.SocketMgr;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.Map;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

/**
 * @author 41784
 */
public class OnMethodCallDelegate {

    void dispatch(MethodCall call, MethodChannel.Result result) {
        switch (call.method) {
            //处理来自Dart的方法调用
            case MethodTypes.INVOKE_DEMO:
                result.success("MethodChannelPlugin收到：" + call.arguments);
                //返回结果给Dart
                MethodChannelPlugin.invokeDartMethod(MethodTypes.INVOKE_DEMO, new MsgBean(1.73245445, "msg"), null);
                break;
            case MethodTypes.OPEN_AGENT_WEB:
                result.success("MethodChannelPlugin收到：" + call.arguments);
                //返回结果给Dart
                AgentWebActivity.openWebView(MainActivity.instance, call.arguments.toString());
                break;
            case MethodTypes.CHECK_UPDATE:
                BuglyManager.checkUpdate();
                break;
            case MethodTypes.ERR_REPORT:
                String errType = "";
                String err = "";
                String stackTrace = "";
                Map<String, String> extraInfo = null;
                if (call.hasArgument("type")) {
                    errType = call.argument("type");
                }
                if (call.hasArgument("error")) {
                    err = call.argument("error");
                }
                if (call.hasArgument("stackTrace")) {
                    stackTrace = call.argument("stackTrace");
                }
                if (call.hasArgument("extraInfo")) {
                    extraInfo = call.argument("extraInfo");
                }
                int category = 8;
                CrashReport.postException(category, errType, err, stackTrace, extraInfo);
                break;
            case MethodTypes.SOCKET_CREATE:
                BeanUser.user_id = call.argument("user_id");
                BeanUser.user_name = call.argument("user_name");
                BeanUser.user_mobile = call.argument("user_mobile");
                BeanUser.agent_id = call.argument("agent_id");
                BeanUser.wall_ids = call.argument("wall_ids");
                BeanUser.role_id = call.argument("role_id");
                SocketMgr.create(call.argument("url"), call.argument("token"));
                result.success(true);
                break;
            case MethodTypes.SOCKET_CLOSE:
                SocketMgr.dispose();
                break;
            case MethodTypes.SOCKET_SEND_MSG:
                SocketMgr.sendMsg(call.arguments.toString());
                break;
            case MethodTypes.TTS_SPEAK:
                NativeTTSMgr.initAndSpeak(call.arguments.toString());
                break;
            case MethodTypes.TTS_STOP:
                NativeTTSMgr.stop();
                break;
            case MethodTypes.START_LOCATING:
                LocationMgr.startLocating();
                break;
            case MethodTypes.STOP_LOCATING:
                LocationMgr.stopLocating();
                break;
            case MethodTypes.SHOW_MAP:
                if (BeanUser.role_id!= 3) {
                    Toast.makeText(MyApplication.getInstance(),"仅代理商可查看",Toast.LENGTH_SHORT).show();
                    return;
                }
                MapActivity.showMapAct(MainActivity.instance);
                break;
            case MethodTypes.OPEN_APP_BY_PACKAGE_NAME:
                String packageName=call.argument("packageName");
                Intent intent = MyApplication.getInstance().getPackageManager().getLaunchIntentForPackage(packageName);
                MyApplication.getInstance().startActivity(intent);
                break;
            default:
                result.notImplemented();
        }
    }
}



